var sellerCode="";
$(document).ready(function() {
	//top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值

	
	$( "#sellers" ).autocomplete({
		source: function( request, response ) {
	        $.ajax({
	          url: contextPath + '/iccard/report/getPackages',
	          dataType: "json",
	          data: {
	            maxRows: 10,
	            keyword: request.term
	          },
	          success: function( data ) {
	        	sellerCode="";
	            response( $.map( data, function( item ) {
	              return {
	                label: item.value+','+item.label,
	                value: item.label
	              };
	            }));
	          }
	        });
	      },
	      minLength: 2,
	      select: function( event, ui ) {
		        sellerCode=ui.item.label;
	      },
	      open: function() {
	        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
	      },
	      close: function() {
	        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
	      }
	    });
	    
});

function doSearch(){
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	
	$("#data_list").datagrid({
		url : contextPath+"/iccard/report/bznis/package/report",
		onLoadError : function() {
			uppAjaxError(data);
		},
		onLoadSuccess:function(data){
			$("#export_link").attr('href',contextPath+'/iccard/report/temp/'+data.fileName);
		},
		queryParams : {
			packageId : sellerCode,
			beginTime : s,
			endTime : e,
			status : 0
		},
		onLoadSuccess: function (data) {
			paint();
		},
		columns : [ [ {
			field : 'comboCardEarningsSumId',
			title : '',
			hidden : true
		},{
			field : 'comboCardName',
			title : '套餐名称'
		}, {
			field : 'begintime',
			title : '套餐开始时间',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		}, {
			field : 'endtime',
			title : '套餐结束时间',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		}, {
			field : 'comboCardExperseAmount',
			title : '销售额'
		}, {
			field : 'comboCardExpectedEarnings',
			title : '预期收益额'
		},{
			field : 'comboCardId',
			title : '操作',
			formatter : function(value, row, index) {
				return "<input type=\"button\" value=\"查看明细\" onclick=\"showDetail("+row.comboCardEarningsSumId+",'"+row.comboCardName+"');\" />";
			}
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		nowrap : false,
		singleSelect : true,
		rownumbers : true
	});
	
	//汇总
	$.ajax({
        url: contextPath + '/iccard/report/bznis/package/actSum',
        dataType: "json",
        data: {
			packageId : sellerCode,
			beginTime : s,
			endTime : e,
			status : 0
		},
        success: function( data ) {
        	$("#collect_amount").text("套餐卡销售总额 ："+data.salesTotal+"元                  预期收益总额 ： "+data.expectedTotal+"元");
        }
      });
	//
}

function showDetail(id,name){
	var _title = "活动结束套餐收益明细";
	var _href = contextPath
			+ "/iccard/report/bznis/package/act_detail.jsp?cardId=" + id
			+ "&cardName=" + name;
	parent.addTab(_title,_href);
}

function paint(){
	var data=$("#data_list").datagrid("getRows");
//	alert(data);
	//套餐
	var packages = new Array();
	//销售
	var sales = new Array();
	//收益
	var income = new Array();
	//销售总额
	var sales_total =0.0;
	//收益总额
	var income_total =0.0;
	//chat
	var chat_sales = new Array("",0.0);
	var chat_income = new Array("",0.0);
	for(var i = 0;i<data.length;i++){
		packages[i] = data[i].comboCardName;
		sales[i] = data[i].comboCardExperseAmount;
		income[i] = data[i].comboCardExpectedEarnings;
		sales_total += data[i].comboCardExperseAmount;
		income_total += data[i].comboCardExpectedEarnings;
	}
	for(var i = 0;i<sales.length;i++){
		chat_sales[i] =new Array( packages[i],100*sales[i]/sales_total);
		chat_income[i] =new Array( packages[i],100*income[i]/income_total);
	}
    //饼图
    $('#sales_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '销售额'
        },
        subtitle: {
            text: ''
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: chat_sales
        }]
    });
    
    $('#income_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '收益额'
        },
        subtitle: {
            text: ''
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: chat_income
        }]
    });
}
